def create_media_structure(video_clips=None, image_clips=None, pip_clips=None):
    media_structure = {"VideoTracks": [], "AudioTracks": []}

    # Helper function to validate and format clips
    def process_clips(clips, required_fields, clip_type):
        processed_clips = []
        for clip in clips:
            for field in required_fields:
                if clip.get(field) is None:
                    raise ValueError(f"Each {clip_type} clip must include '{field}' field.")

            # Include 'Type' for image clips
            if clip_type == "Image":
                clip_data = {"Type": "Image"}
            elif clip_type == "Audio":
                clip_data = {}
            else:
                clip_data = {}

            # Add all fields if they are not None
            clip_data.update({k: clip.get(k) for k in clip if clip.get(k) is not None})
            processed_clips.append(clip_data)

        return processed_clips

    # Main Video Clips
    if video_clips:
        try:
            video_clips_data = process_clips(video_clips, ["In", "Out"], "Video")
            media_structure["VideoTracks"].append({"VideoTrackClips": video_clips_data})
        except ValueError as e:
            return {"error": str(e),"data":""}

    # Image Clips
    if image_clips:
        try:
            image_clips_data = process_clips(image_clips, ["TimelineIn", "Duration"], "Image")
            media_structure["VideoTracks"].append({"VideoTrackClips": image_clips_data})
        except ValueError as e:
            return {"error": str(e),"data":""}

    # Picture-in-Picture Clips
    if pip_clips:
        try:
            pip_clips_data = process_clips(pip_clips,required_fields=[],clip_type="PIP")
            media_structure["VideoTracks"].append({"VideoTrackClips": pip_clips_data})
        except ValueError as e:
            return {"error": str(e),"data":""}
    else:
        # Ensure PIP track is present as an empty track if no clips are provided
        media_structure["VideoTracks"].append({"VideoTrackClips": []})

    # Audio Clips
    # if audio_clips:
    #     try:
    #         audio_clips_data = process_clips(audio_clips, ["TimelineIn",], "Audio")
    #         media_structure["AudioTracks"].append({"AudioTrackClips": audio_clips_data})
    #     except ValueError as e:
    #         return {"error": str(e),"data":""}
    # else:
    #     # Ensure Audio track is present as an empty track if no clips are provided
    #     media_structure["AudioTracks"].append({"AudioTrackClips": []})

    # Return success response
    return {"success": "Media structure created successfully.", "data": media_structure}


# # Example usage:
# audio_clips = [
#     {
#         "MediaURL": "https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/music/m2.wav",
#
#         "In": 4,
#         "Out": 10,
#
#     },   {
#         "MediaURL": "https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/music/m2.wav",
#
#         "In": 4,
#         "Out": 10,
#
#     }
# ]
#
# result = create_media_structure(audio_clips=audio_clips)
# print(result)
